#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import hashlib
import random
import json
import time


def get_sign(body:dict):
    chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    # charLens = len(chars)
    if isinstance(body, str):  # Use `str` instead of `basestring` (Python 3)
        body = json.loads(body)

    if body.get('sign') is None:  # Fixed condition and indentation
        req_body = body.copy()
        for key in body:
            value = body[key]
            if value != None:  # Fixed indentation
                if isinstance(value, dict) or isinstance(value, list):  # Added colon
                    value = json.dumps(value, ensure_ascii=False, separators=(',', ':'), indent=None)
                req_body[key] = value

        req_body["timestamp"] = str(int(time.time()))
        resRandom = ''.join(random.choice(chars) for _ in range(16))
        req_body["noncestr"] = resRandom
        keys = list(req_body.keys())
        keys.sort()
        vs = []
        for key in keys:
            vs.append("{}={}".format(key, req_body[key]))
        vs.append("sign_key=31DA13991989F04670A3D0DEDC6CC066FAA655D6")

        csignstr = "&".join(vs)
        sign = hashlib.sha256(csignstr.encode('utf-8')).hexdigest().upper()  # Encoding the string before hashing
        return sign
                

